using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Diagnostics;
using System.Windows.Forms;

using Janus.Windows.EditControls;
using Janus.Windows.Schedule;
namespace Janus.NorthwindDemo2007
{
	public partial class RecurrenceForm
	{

		private RecurrencePattern mRecurrencePattern = null;
		private ScheduleAppointment mAppointment = null;

		protected override void OnLoad(System.EventArgs e)
		{
			base.OnLoad(e);
			NorthwindApp.MainForm.VisualStyleManager1.AddControl(this, true);
		}
		public void ShowRecurrenceForm(ScheduleAppointment appointment)
		{

			this.PopulateCombos();
			this.mAppointment = appointment;
			if (this.mAppointment.RecurrencePattern != null)
			{
				this.mRecurrencePattern = appointment.RecurrencePattern;
			}
			else
			{
				this.mRecurrencePattern = new RecurrencePattern();
			}
			this.SetDefaultValues();
			this.ShowDialog();
		}

		private ScheduleDayOfWeek GetSelectedWeekDays()
		{
			ScheduleDayOfWeek daysOfWeek = 0;
			if (this.chkMonday.Checked)
			{
				daysOfWeek = daysOfWeek | ScheduleDayOfWeek.Monday;
			}
			if (this.chkTuesday.Checked)
			{
				daysOfWeek = daysOfWeek | ScheduleDayOfWeek.Tuesday;
			}
			if (this.chkWednesday.Checked)
			{
				daysOfWeek = daysOfWeek | ScheduleDayOfWeek.Wednesday;
			}
			if (this.chkThursday.Checked)
			{
				daysOfWeek = daysOfWeek | ScheduleDayOfWeek.Thursday;
			}
			if (this.chkFriday.Checked)
			{
				daysOfWeek = daysOfWeek | ScheduleDayOfWeek.Friday;
			}
			if (this.chkSaturday.Checked)
			{
				daysOfWeek = daysOfWeek | ScheduleDayOfWeek.Saturday;
			}
			if (this.chkSunday.Checked)
			{
				daysOfWeek = daysOfWeek | ScheduleDayOfWeek.Sunday;
			}
			return daysOfWeek;
		}

		private void SelectDaysOfWeek()
		{

			if ((this.mRecurrencePattern.DayOfWeek & ScheduleDayOfWeek.Monday) != 0)
			{
				this.chkMonday.Checked = true;
			}
			else
			{
				this.chkMonday.Checked = false;
			}

			if ((this.mRecurrencePattern.DayOfWeek & ScheduleDayOfWeek.Tuesday) != 0)
			{
				this.chkTuesday.Checked = true;
			}
			else
			{
				this.chkTuesday.Checked = false;
			}

			if ((this.mRecurrencePattern.DayOfWeek & ScheduleDayOfWeek.Wednesday) != 0)
			{
				this.chkWednesday.Checked = true;
			}
			else
			{
				this.chkWednesday.Checked = false;
			}

			if ((this.mRecurrencePattern.DayOfWeek & ScheduleDayOfWeek.Thursday) != 0)
			{
				this.chkThursday.Checked = true;
			}
			else
			{
				this.chkThursday.Checked = false;
			}

			if ((this.mRecurrencePattern.DayOfWeek & ScheduleDayOfWeek.Friday) != 0)
			{
				this.chkFriday.Checked = true;
			}
			else
			{
				this.chkFriday.Checked = false;
			}

			if ((this.mRecurrencePattern.DayOfWeek & ScheduleDayOfWeek.Saturday) != 0)
			{
				this.chkSaturday.Checked = true;
			}
			else
			{
				this.chkSaturday.Checked = false;
			}

			if ((this.mRecurrencePattern.DayOfWeek & ScheduleDayOfWeek.Sunday) != 0)
			{
				this.chkSunday.Checked = true;
			}
			else
			{
				this.chkSunday.Checked = false;
			}
		}

		private void SetDefaultValues()
		{
			this.btnRemoveRecurrence.Enabled = this.mAppointment.RecurrencePattern != null;

			if (this.mAppointment.RecurrencePattern != null)
			{
				this.jccRecurrenceEndDate.Value = mRecurrencePattern.PatternEndDate;
				this.jccRecurrenceStart.Value = mRecurrencePattern.PatternStartDate;

				this.jccStartTime.Value = jccRecurrenceStart.Value.Date.Add(mRecurrencePattern.StartTime);
				this.jccEndTime.Value = jccRecurrenceStart.Value.Date.Add(mRecurrencePattern.EndTime);
				this.numOccurrences.Value = mRecurrencePattern.Occurrences;

                this.mRecurrencePattern.SetDefaultValuesForDate(this.mAppointment.StartTime.Date);
				switch (this.mRecurrencePattern.RecurrenceType)
				{
					case RecurrenceType.Daily:
						this.rdbDaily.Checked = true;
						break;
					case RecurrenceType.Weekly:
						this.rdbWeekly.Checked = true;
						this.SelectDaysOfWeek();
						this.numWeeklyInterval.Value = this.mRecurrencePattern.Interval;
						break;
					case RecurrenceType.Monthly:
						this.rdbMonthly.Checked = true;
						break;
					case RecurrenceType.MontNth:
						this.rdbMonthNth.Checked = true;
						this.rdbMonthly.Checked = true;
						break;
					case RecurrenceType.Yearly:
						this.rdbYearly.Checked = true;
						break;
					case RecurrenceType.YearNth:
						this.rdbYearNth.Checked = true;
						this.rdbYearly.Checked = true;
						break;
				}

				if (this.mRecurrencePattern.RecurrenceEndMode == RecurrenceEndMode.EndDate)
				{
					this.rdbPatternEndDate.Checked = true;
				}
				else if (this.mRecurrencePattern.RecurrenceEndMode == RecurrenceEndMode.Occurrences)
				{
					this.rdbOccurrences.Checked = true;
				}
			}
			else
			{
				this.jccStartTime.Value = mAppointment.StartTime;
				this.jccEndTime.Value = mAppointment.EndTime;
				this.jccRecurrenceStart.Value = mAppointment.StartTime.Date;
				this.jccRecurrenceEndDate.Value = this.jccRecurrenceStart.Value.AddDays(9 * 7);
				this.rdbWeekly.Checked = true;
				this.mRecurrencePattern.SetDefaultValuesForDate(this.jccRecurrenceStart.Value);
				this.SelectDaysOfWeek();
			}
		}

		private void PopulateCombos()
		{

			ScheduleDayOfWeek dayOption = ScheduleDayOfWeek.Monday | ScheduleDayOfWeek.Tuesday | ScheduleDayOfWeek.Wednesday | ScheduleDayOfWeek.Thursday | ScheduleDayOfWeek.Friday | ScheduleDayOfWeek.Saturday | ScheduleDayOfWeek.Sunday;

			ScheduleDayOfWeek weekDayOption = ScheduleDayOfWeek.Monday | ScheduleDayOfWeek.Tuesday | ScheduleDayOfWeek.Wednesday | ScheduleDayOfWeek.Thursday | ScheduleDayOfWeek.Friday;

			ScheduleDayOfWeek weekEndDayOption = ScheduleDayOfWeek.Saturday | ScheduleDayOfWeek.Sunday;

			this.cboMonthlyDayWeek.Items.Add("Day", dayOption);
			this.cboMonthlyDayWeek.Items.Add("Week day", weekDayOption);
			this.cboMonthlyDayWeek.Items.Add("Weekend day", weekEndDayOption);
			this.cboMonthlyDayWeek.Items.Add("Sunday", ScheduleDayOfWeek.Sunday);
			this.cboMonthlyDayWeek.Items.Add("Monday", ScheduleDayOfWeek.Monday);
			this.cboMonthlyDayWeek.Items.Add("Tuesday", ScheduleDayOfWeek.Tuesday);
			this.cboMonthlyDayWeek.Items.Add("Wednesday", ScheduleDayOfWeek.Wednesday);
			this.cboMonthlyDayWeek.Items.Add("Thursday", ScheduleDayOfWeek.Thursday);
			this.cboMonthlyDayWeek.Items.Add("Friday", ScheduleDayOfWeek.Friday);
			this.cboMonthlyDayWeek.Items.Add("Saturday", ScheduleDayOfWeek.Saturday);

			this.cboMonthlyDayWeek.DisplayMember = "Text";
			this.cboMonthlyDayWeek.ValueMember = "Value";

			this.cboYearlyDayOfWeek.Items.Add("Day", dayOption);
			this.cboYearlyDayOfWeek.Items.Add("Week day", weekDayOption);
			this.cboYearlyDayOfWeek.Items.Add("Weekend day", weekEndDayOption);
			this.cboYearlyDayOfWeek.Items.Add("Sunday", ScheduleDayOfWeek.Sunday);
			this.cboYearlyDayOfWeek.Items.Add("Monday", ScheduleDayOfWeek.Monday);
			this.cboYearlyDayOfWeek.Items.Add("Tuesday", ScheduleDayOfWeek.Tuesday);
			this.cboYearlyDayOfWeek.Items.Add("Wednesday", ScheduleDayOfWeek.Wednesday);
			this.cboYearlyDayOfWeek.Items.Add("Thursday", ScheduleDayOfWeek.Thursday);
			this.cboYearlyDayOfWeek.Items.Add("Friday", ScheduleDayOfWeek.Friday);
			this.cboYearlyDayOfWeek.Items.Add("Saturday", ScheduleDayOfWeek.Saturday);

			this.cboYearlyDayOfWeek.DisplayMember = "Text";
			this.cboYearlyDayOfWeek.ValueMember = "Value";

			this.cboMonthlyOccurrence.Items.Add(Janus.Windows.Schedule.OccurrenceInMonth.First);
			this.cboMonthlyOccurrence.Items.Add(Janus.Windows.Schedule.OccurrenceInMonth.Second);
			this.cboMonthlyOccurrence.Items.Add(Janus.Windows.Schedule.OccurrenceInMonth.Third);
			this.cboMonthlyOccurrence.Items.Add(Janus.Windows.Schedule.OccurrenceInMonth.Fourth);
			this.cboMonthlyOccurrence.Items.Add(Janus.Windows.Schedule.OccurrenceInMonth.Last);

			this.cboMonthlyOccurrence.SelectedIndex = 0;
			this.cboMonthlyOccurrence.ValueMember = "Value";

			this.cboYearlyOccurrence.Items.Add(Janus.Windows.Schedule.OccurrenceInMonth.First);
			this.cboYearlyOccurrence.Items.Add(Janus.Windows.Schedule.OccurrenceInMonth.Second);
			this.cboYearlyOccurrence.Items.Add(Janus.Windows.Schedule.OccurrenceInMonth.Third);
			this.cboYearlyOccurrence.Items.Add(Janus.Windows.Schedule.OccurrenceInMonth.Fourth);
			this.cboYearlyOccurrence.Items.Add(Janus.Windows.Schedule.OccurrenceInMonth.Last);

			this.cboYearlyOccurrence.SelectedIndex = 0;
			this.cboYearlyOccurrence.ValueMember = "Value";

			this.cboMonthOfYear.Items.Add("January", 1);
			this.cboMonthOfYear.Items.Add("February", 2);
			this.cboMonthOfYear.Items.Add("March", 3);
			this.cboMonthOfYear.Items.Add("April", 4);
			this.cboMonthOfYear.Items.Add("May", 5);
			this.cboMonthOfYear.Items.Add("June", 6);
			this.cboMonthOfYear.Items.Add("July", 7);
			this.cboMonthOfYear.Items.Add("August", 8);
			this.cboMonthOfYear.Items.Add("September", 9);
			this.cboMonthOfYear.Items.Add("October", 10);
			this.cboMonthOfYear.Items.Add("November", 11);
			this.cboMonthOfYear.Items.Add("December", 12);
			this.cboMonthOfYear.DisplayMember = "Text";
			this.cboMonthOfYear.ValueMember = "Value";

			this.cboMonthOfYearNth.Items.Add("January", 1);
			this.cboMonthOfYearNth.Items.Add("February", 2);
			this.cboMonthOfYearNth.Items.Add("March", 3);
			this.cboMonthOfYearNth.Items.Add("April", 4);
			this.cboMonthOfYearNth.Items.Add("May", 5);
			this.cboMonthOfYearNth.Items.Add("June", 6);
			this.cboMonthOfYearNth.Items.Add("July", 7);
			this.cboMonthOfYearNth.Items.Add("August", 8);
			this.cboMonthOfYearNth.Items.Add("September", 9);
			this.cboMonthOfYearNth.Items.Add("October", 10);
			this.cboMonthOfYearNth.Items.Add("November", 11);
			this.cboMonthOfYearNth.Items.Add("December", 12);
			this.cboMonthOfYearNth.DisplayMember = "Text";
			this.cboMonthOfYearNth.ValueMember = "Value";
		}

		private bool SetRecurrencePatternValues()
		{

			if (this.mAppointment.RecurrencePattern == null)
			{
				this.mAppointment.RecurrencePattern = this.mRecurrencePattern;
			}

			mRecurrencePattern.BeginEdit();
			try
			{
				mRecurrencePattern.StartTime = this.jccStartTime.Value.TimeOfDay;
				TimeSpan endTime = new TimeSpan(this.mAppointment.Duration.Days, 0, 0, 0);
				endTime = endTime.Add(this.jccEndTime.Value.TimeOfDay);

				mRecurrencePattern.EndTime = endTime;
				mRecurrencePattern.PatternStartDate = this.jccRecurrenceStart.Value;
				mRecurrencePattern.PatternEndDate = this.jccRecurrenceEndDate.Value;

				if (this.rdbDaily.Checked)
				{
					mRecurrencePattern.RecurrenceType = RecurrenceType.Daily;
					mRecurrencePattern.Interval = (int)this.numDailyInterval.Value;
					if (this.rdbWeekDay.Checked)
					{
						mRecurrencePattern.DailyRecurrenceMode = DailyRecurrenceMode.WeekDays;
					}
					else if (this.rdbWeekendDay.Checked)
					{
						mRecurrencePattern.DailyRecurrenceMode = DailyRecurrenceMode.WeekendDays;
					}
					else
					{
						mRecurrencePattern.DailyRecurrenceMode = DailyRecurrenceMode.UseInterval;
					}

				}
				else if (this.rdbWeekly.Checked)
				{
					mRecurrencePattern.RecurrenceType = RecurrenceType.Weekly;
					mRecurrencePattern.Interval = (int)this.numWeeklyInterval.Value;
					mRecurrencePattern.DayOfWeek = this.GetSelectedWeekDays();
				}
				else if (this.rdbMonthly.Checked)
				{
					if (this.rdbMonthNth.Checked)
					{
						mRecurrencePattern.RecurrenceType = RecurrenceType.MontNth;
						mRecurrencePattern.OccurrenceInMonth = (OccurrenceInMonth)this.cboMonthlyOccurrence.SelectedValue;
						mRecurrencePattern.DayOfWeek = (ScheduleDayOfWeek)this.cboMonthlyDayWeek.SelectedValue;
						mRecurrencePattern.Interval = (int)this.numMonthNthInterval.Value;
					}
					else
					{
						mRecurrencePattern.RecurrenceType = RecurrenceType.Monthly;
						mRecurrencePattern.DayOfMonth = (int)this.numDayOfMonth.Value;
						mRecurrencePattern.Interval = (int)this.numMonthlyInterval.Value;
					}
				}
				else if (this.rdbYearly.Checked)
				{
					if (this.rdbYearNth.Checked)
					{
						mRecurrencePattern.RecurrenceType = RecurrenceType.YearNth;
						mRecurrencePattern.MonthOfYear = (int)this.cboMonthOfYearNth.SelectedValue;
						mRecurrencePattern.OccurrenceInMonth = (OccurrenceInMonth)this.cboYearlyOccurrence.SelectedValue;
						mRecurrencePattern.DayOfWeek = (ScheduleDayOfWeek)this.cboYearlyDayOfWeek.SelectedValue;
					}
					else
					{
						mRecurrencePattern.RecurrenceType = RecurrenceType.Yearly;
						mRecurrencePattern.MonthOfYear = (int)this.cboMonthOfYear.SelectedValue;
						mRecurrencePattern.DayOfMonth = (int)this.numYearlyDayOfMonth.Value;
					}
				}

				if (this.rdbNoEndDate.Checked)
				{
					mRecurrencePattern.RecurrenceEndMode = RecurrenceEndMode.NoEndDate;
				}
				else if (this.rdbOccurrences.Checked)
				{
					mRecurrencePattern.RecurrenceEndMode = RecurrenceEndMode.Occurrences;
					mRecurrencePattern.Occurrences = (int)this.numOccurrences.Value;
				}
				else
				{
					mRecurrencePattern.RecurrenceEndMode = RecurrenceEndMode.EndDate;
					mRecurrencePattern.PatternEndDate = this.jccRecurrenceEndDate.Value;
				}

			}
			catch
			{
				MessageBox.Show(this, "The recurrence pattern is not valid", NorthwindApp.MessageCaption, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
				return false;
			}

			mRecurrencePattern.EndEdit();
			return true;
		}

		private void btnOK_Click(object sender, System.EventArgs e)
		{
			try
			{
				if (this.mRecurrencePattern != null && this.mRecurrencePattern.Exceptions.Count > 0)
				{
					DialogResult result = MessageBox.Show(this, "Any exceptions associated with this recurring appointment will be lost. Is this OK?", NorthwindApp.MessageCaption, MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation);
					if (result == System.Windows.Forms.DialogResult.Cancel)
					{
						return;
					}
				}
				if (this.SetRecurrencePatternValues())
				{
					this.Close();
				}
			}
			catch
			{
				MessageBox.Show("The recurrence pattern is not valid", NorthwindApp.MessageCaption);
			}

		}

		private void btnCancel_Click(object sender, System.EventArgs e)
		{
			this.Close();
		}

		private void rdbDaily_CheckedChanged(object sender, System.EventArgs e)
		{
			this.grbDailyPattern.Visible = this.rdbDaily.Checked;
			if (rdbDaily.Checked)
			{
				this.numDailyInterval.Value = this.mRecurrencePattern.Interval;
				switch (this.mRecurrencePattern.DailyRecurrenceMode)
				{
					case DailyRecurrenceMode.UseInterval:
						this.rdbDailyInterval.Checked = true;
						break;
					case DailyRecurrenceMode.WeekDays:
						this.rdbWeekDay.Checked = true;
						break;
					case DailyRecurrenceMode.WeekendDays:
						this.rdbWeekendDay.Checked = true;
						break;
				}
			}
		}

		private void rdbWeekly_CheckedChanged(object sender, System.EventArgs e)
		{
			this.grbWeeklyPattern.Visible = this.rdbWeekly.Checked;
			if (mRecurrencePattern != null && rdbWeekly.Checked)
			{
				this.numWeeklyInterval.Value = this.mRecurrencePattern.Interval;
				this.SelectDaysOfWeek();
			}
		}

		private void rdbMonthly_CheckedChanged(object sender, System.EventArgs e)
		{
			this.grbMonthlyPattern.Visible = this.rdbMonthly.Checked;
			if (rdbMonthly.Checked)
			{
				this.numDayOfMonth.Value = this.mRecurrencePattern.DayOfMonth;
				this.cboMonthlyDayWeek.SelectedValue = this.mRecurrencePattern.DayOfWeek;
				this.cboMonthlyOccurrence.SelectedValue = this.mRecurrencePattern.OccurrenceInMonth;
				this.numMonthlyInterval.Value = this.mRecurrencePattern.Interval;
				this.numMonthNthInterval.Value = this.mRecurrencePattern.Interval;
			}
		}

		private void rdbYearly_CheckedChanged(object sender, System.EventArgs e)
		{
			this.grbYearlyPattern.Visible = this.rdbYearly.Checked;
			if (rdbYearly.Checked)
			{
				this.numYearlyDayOfMonth.Value = this.mRecurrencePattern.DayOfMonth;
				this.cboYearlyDayOfWeek.SelectedValue = this.mRecurrencePattern.DayOfWeek;
				this.cboMonthOfYear.SelectedValue = this.mRecurrencePattern.MonthOfYear;
				this.cboMonthOfYearNth.SelectedValue = this.mRecurrencePattern.MonthOfYear;
				this.cboYearlyOccurrence.SelectedValue = this.mRecurrencePattern.OccurrenceInMonth;
			}
		}

		private void btnRemoveRecurrence_Click(object sender, System.EventArgs e)
		{
			this.mAppointment.RecurrencePattern = null;
			this.Close();
		}

		private void jccRecurrenceStart_ValueChanged(object sender, System.EventArgs e)
		{
			jccRecurrenceEndDate.Value = jccRecurrenceStart.Value.AddDays(9 * 7);
		}
	}

} //end of root namespace